package br.ufrpe.bsi.pvs.xml.request;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;

import br.ufrpe.bsi.pvs.control.BSFactory;
import br.ufrpe.bsi.pvs.control.funcionario.FuncionarioBs;
import br.ufrpe.bsi.pvs.control.roupa.RoupaBs;
import br.ufrpe.bsi.pvs.model.funcionario.Funcionario;
import br.ufrpe.bsi.pvs.model.roupa.Roupa;
import br.ufrpe.bsi.pvs.xml.XMLUtil;
import br.ufrpe.bsi.pvs.xml.response.ListedResponseProcess;
import br.ufrpe.bsi.pvs.xml.transformer.FuncionarioXML;
import br.ufrpe.bsi.pvs.xml.transformer.QueryXML;
import br.ufrpe.bsi.pvs.xml.transformer.RoupaXML;

public class ListRequestProcessor implements RequestProcessor {

	@Override
	public void process(Element request) {
		Element classe = request.getChild(XMLUtil.ELEM_CLASS);
		if (classe.getAttribute(XMLUtil.ATTR_NAME).getValue()
				.equals("funcionario")) {
			FuncionarioBs bs = BSFactory.getFuncionarioBs();
			List<Funcionario> listaF = new ArrayList<Funcionario>();
			listaF = bs.listar();
			
			XMLUtil.writeResponse(FuncionarioXML.createResponseDocument(listaF));
		}else{
			RoupaBs bs = BSFactory.getRoupaBs();
			List<Roupa> listaR = new ArrayList<Roupa>();
			listaR = bs.listar();
			
			XMLUtil.writeResponse(RoupaXML.createResponseDocument(listaR));
 		}

	}

	/** generates a default add request xml document */
	public static Document getListRequestDocument(String className) {
		Document document = new Document();

		Element root = new Element(XMLUtil.ELEM_ROOT);
		Element request = new Element(XMLUtil.ELEM_REQUEST);

		Attribute nomerequest = new Attribute(XMLUtil.ATTR_NAME, XMLUtil.LIST);
		request.setAttribute(nomerequest);
		root.addContent(request);

		Element classe = new Element(XMLUtil.ELEM_CLASS);
		Attribute nomeclasse = new Attribute(XMLUtil.ATTR_NAME, className);
		classe.setAttribute(nomeclasse);

		request.setContent(classe);
		document.setRootElement(root);

		return document;
	}


}
